﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;

namespace WebBanThuoc.ws
{
    /// <summary>
    /// Summary description for CKEditorUpdateFile
    /// </summary>
    public class CKEditorUpdateFile : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string str_UploadFolder = System.Configuration.ConfigurationManager.AppSettings["AnhTinTuc"];
            string str_FolderPath_Server = context.Server.MapPath(str_UploadFolder);

            HttpPostedFile filesUpdate = context.Request.Files["upload"];
            string CKEditorFuncNum = context.Request["CKEditorFuncNum"];
            string fileName = DateTime.Now.ToString("dd_MM_yyyy__HH_mm_ss_") + filesUpdate.FileName;

            if (!Directory.Exists(str_FolderPath_Server))
            {
                Directory.CreateDirectory(str_FolderPath_Server);
            }

            filesUpdate.SaveAs(str_FolderPath_Server + "/" + fileName);
            string url = str_UploadFolder.Replace("..", "") +"/" + fileName;
            context.Response.Write("<script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\");</script>");
            context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}